﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class BillNumberHelper
    {

        /// <summary>
        /// 今天过磅的数量，用于磅单排序
        /// </summary>
        private static Dictionary<WeightingBillType, int>? TodayCountDic = new();

        #region update today count

        /// <summary>
        /// update today count
        /// </summary>
        /// <param name="type"> weighing Type tostring</param>
        /// <param name="value"> 1 or -1</param>
        public static void UpdateTodayCount(WeightingBillType type, bool isAdd = true)
        {
            var value = isAdd ? 1 : 0;
            if (TodayCountDic.ContainsKey(type))
            {
                var v = TodayCountDic[type];
                TodayCountDic[type] = v + value;
            }
            else
            {
                TodayCountDic.Add(type, value);
            }
        }

        public static void UpdateTodayCount(WeightingBillType type, int value)
        {
            if (TodayCountDic.ContainsKey(type))
            {
                var v = TodayCountDic[type];
                TodayCountDic[type] = v + value;
            }
            else
            {
                TodayCountDic.Add(type, value);
            }
        }
        public static int GetTodayCount(WeightingBillType type)
        {
            if (TodayCountDic.ContainsKey(type))
            {
                var v = TodayCountDic[type];
                return v;
            }
            else
            {
                return 0;
            }
        }

        #endregion

        /// <summary>
        /// Get Weighing Number 获取磅单
        /// </summary>
        /// <param name="WeightingBillType"> CK OR RK</param>
        /// <param name="ExtStr"> BL OR WL</param>      
        /// <returns></returns>
        public static String GetWeighingNumber(WeightingBillType type, String ExtStr = null)
        {
            String dateStr = DateTimeHelper.getCurrentDateTime(DateTimeHelper.BillNumberDateTimeFormat);
            if (App.Config.BillPrintConfig.NumberMode == BillNumberMode.OnlyNumber)
            {
                var n = WeighingBillDao.Instance.GetTodayCount();
                return dateStr + GetSort(n);
            }
            String typeText = GetTypeHeader(type);
            String header = App.Config.BillPrintConfig.Header;
            int count = 0;
            //TODO multiple computer mybe repeated Number
            if (App.Config.WeighingConfig.MultipleMachines == false)
            {               
                if(ExtStr == "BL")
                {
                    count = 0;
                }
                else
                {
                    count = GetTodayCount(type);
                }
                    
            }

            if (count == 0)
            {
                //才启动
                count = WeighingBillDao.Instance.GetTodayCount(type);
                int temp = 0;
                WeighingBill LastBill = WeighingBillDao.Instance.FindLastByType(type, true);
                if (LastBill != null)
                {
                    string number = LastBill.number;
                    int order = 0;
                    string str = number.Substring(number.Length - 4, 4);
                    try
                    {
                        order = Convert.ToInt32(str);
                    }
                    catch (Exception)
                    {
                    }
                    temp = Math.Max(order, count);
                }
                if (temp > count) { count = temp; }

                UpdateTodayCount(type, count);
            }
            string sort = GetSort(count);
            if (!string.IsNullOrEmpty(header)) header = "-" + header;
            if (!String.IsNullOrEmpty(ExtStr)) ExtStr = "-" + ExtStr.ToUpper();
            var mid = "";
            if (App.Config.WeighingConfig.MultipleMachines)
            {
                mid = App.InitConfig.machineId > 9 ? App.InitConfig.machineId.ToString() :("0" + App.InitConfig.machineId);
            }
            return typeText + ExtStr + header + "-" + dateStr+ mid + sort;
        }

        private static string GetSort(int count)
        {
            string sort;
            switch (count.ToString().Length)
            {
                case 1:
                    sort = "000" + (count + 1).ToString();
                    break;
                case 2:
                    sort = "00" + (count + 1).ToString();
                    break;
                case 3:
                    sort = "0" + (count + 1).ToString();
                    break;
                default:
                    sort = (count + 1).ToString();
                    break;
            }
            return sort;
        }

        private static string GetTypeHeader(WeightingBillType type)
        {
            string res;
            switch (type)
            {
                case WeightingBillType.Buy:
                    res = "CG";
                    break;
                case WeightingBillType.Sale:
                    res = "XS";
                    break;
                case WeightingBillType.Self:
                    res = "NBDY";
                    break;
                case WeightingBillType.Foreign:
                    res = "WLGB";
                    break;
                case WeightingBillType.Mixture:
                    res = "WZ";
                    break;
                default:
                    res = "QT";
                    break;
            }
            return res;
        }

    }
}
